package com.vision.sales.cl.ui;

import com.google.gwt.core.client.GWT;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.container.CardLayoutContainer;
import com.vision.core.cl.CodeDownloadCallback;
import com.vision.core.cl.ui.ITabItemPanel;
import com.vision.core.cl.ui.ITabItemPanelBuilder;
import com.vision.sales.advancepayment.cl.ui.AdvancePaymentsPanel;
import com.vision.sales.check.cl.ui.ChecksPanel;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.collection.cl.ui.CollectionsPanel;
import com.vision.sales.creditmemo.cl.ui.CreditMemosPanel;
import com.vision.sales.customer.cl.ui.CustomersPanel;
import com.vision.sales.debitmemo.cl.u.DebitMemosPanel;
import com.vision.sales.deliveryreceipt.cl.ui.DeliveryReceiptsPanel;
import com.vision.sales.monthlytotals.cl.ui.MonthlyTotalsPanel;
import com.vision.sales.product.cl.ui.ProductsPanel;
import com.vision.sales.report.cl.ui.CommissionReportPanel;
import com.vision.sales.report.cl.ui.CustomerAgingReportPanel;
import com.vision.sales.report.cl.ui.CustomerLedgerReportPanel;
import com.vision.sales.report.cl.ui.CustomerSalesReportPanel;
import com.vision.sales.report.cl.ui.DeliveryReceiptsReportPanel;
import com.vision.sales.report.cl.ui.IndividualSOAReportPanel;
import com.vision.sales.report.cl.ui.IndividualSalesmanCommissionReportPanel;
import com.vision.sales.report.cl.ui.IndividualSalesmanSalesReportPanel;
import com.vision.sales.report.cl.ui.MonthlyGroupSalesReportPanel;
import com.vision.sales.report.cl.ui.OutstandingReceivablesReportPanel;
import com.vision.sales.report.cl.ui.ProductSalesReportPanel;
import com.vision.sales.report.cl.ui.SalesmanSalesReportPanel;
import com.vision.sales.salesinvoice.cl.ui.SalesInvoicesPanel;
import com.vision.sales.salesman.cl.ui.SalesmenPanel;

/**
 * Builder class for adding {@link ITabItemPanel} instances into a {@link TabPanel}. 
 * 
 * @author Mark
 *
 */
public class TabItemPanelBuilder implements ITabItemPanelBuilder {
	
	private ITabItemPanel productsPanel;
	private ITabItemPanel customersPanel;
	private ITabItemPanel salesmenPanel;
	private ITabItemPanel checksPanel;
	private ITabItemPanel monthlyTotalsPanel;
	private ITabItemPanel deliveryReceiptsPanel;
	private ITabItemPanel salesInvoicesPanel;
	private ITabItemPanel collectionsPanel;
	private ITabItemPanel advancePaymentsPanel;
	private ITabItemPanel creditMemosPanel;
	private ITabItemPanel debitMemosPanel;
	
	private ITabItemPanel customerLedgerPanel;
	private ITabItemPanel customerSalesPanel;
	private ITabItemPanel productSalesPanel;
	private ITabItemPanel monthlyGroupSalesPanel;
	private ITabItemPanel salesmanSalesReportPanel;
	private ITabItemPanel indivSalesmanSalesReportPanel;
	private ITabItemPanel indivSalesmanCommReportPanel;
	private ITabItemPanel outstandingReceivablesPanel;
	private ITabItemPanel deliveryReceiptsReportPanel;
	private ITabItemPanel individualSOAPanel;
	private ITabItemPanel customerAgingReportPanel;
	private ITabItemPanel commissionReportPanel;

	@Override
	public void create(String id, final Component parentPanel) {
		// PRODUCTS
		if (SalesTasks.PRODUCTS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (productsPanel == null)
						productsPanel = new ProductsPanel();
					activatePanel(parentPanel, productsPanel);
				}
			});
		}
		// CUSTOMERS
		else if (SalesTasks.CUSTOMERS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (customersPanel == null)
						customersPanel = new CustomersPanel();
					activatePanel(parentPanel, customersPanel);
				}
			});
		}
		// SALESMEN
		else if (SalesTasks.SALESMEN.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (salesmenPanel == null)
						salesmenPanel = new SalesmenPanel();
					activatePanel(parentPanel, salesmenPanel);
				}
			});
		}
		// MONTHLY TOTALS
		else if (SalesTasks.MONTHLY_TOTALS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (monthlyTotalsPanel == null)
						monthlyTotalsPanel = new MonthlyTotalsPanel();
					activatePanel(parentPanel, monthlyTotalsPanel);
				}
			});
		}
		// DELIVERY_RECEIPTS
		else if (SalesTasks.DELIVERY_RECEIPTS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (deliveryReceiptsPanel == null)
						deliveryReceiptsPanel = new DeliveryReceiptsPanel();
					activatePanel(parentPanel, deliveryReceiptsPanel);
				}
			});
		}
		// SALES_INVOICES
		else if (SalesTasks.SALES_INVOICES.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (salesInvoicesPanel == null)
						salesInvoicesPanel = new SalesInvoicesPanel();
					activatePanel(parentPanel, salesInvoicesPanel);
				}
			});
		}
		// COLLECTIONS
		else if (SalesTasks.COLLECTIONS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (collectionsPanel == null)
						collectionsPanel = new CollectionsPanel();
					activatePanel(parentPanel, collectionsPanel);
				}
			});
		}
		// ADVANCE_PAYMENTS
		else if (SalesTasks.ADVANCE_PAYMENTS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (advancePaymentsPanel == null)
						advancePaymentsPanel = new AdvancePaymentsPanel();
					activatePanel(parentPanel, advancePaymentsPanel);
				}
			});
		}
		// CREDIT_MEMOS
		else if (SalesTasks.CREDIT_MEMOS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (creditMemosPanel == null)
						creditMemosPanel = new CreditMemosPanel();
					activatePanel(parentPanel, creditMemosPanel);
				}
			});
		}
		// DEBIT_MEMOS
		else if (SalesTasks.DEBIT_MEMOS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (debitMemosPanel == null)
						debitMemosPanel = new DebitMemosPanel();
					activatePanel(parentPanel, debitMemosPanel);
				}
			});
		}
		// CHECKS
		else if (SalesTasks.CHECKS.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (checksPanel == null)
						checksPanel = new ChecksPanel();
					activatePanel(parentPanel, checksPanel);
				}
			});
		}
		// CUSTOMER LEDGER
		else if (SalesTasks.CUSTOMER_LEDGER.getId().equals(id)) {
			if (customerLedgerPanel == null)
				customerLedgerPanel = new CustomerLedgerReportPanel();
			activatePanel(parentPanel, customerLedgerPanel);
		}
		// CUSTOMER SALES
		else if (SalesTasks.CUSTOMER_SALES.getId().equals(id)) {
			if (customerSalesPanel == null)
				customerSalesPanel = new CustomerSalesReportPanel();
			activatePanel(parentPanel, customerSalesPanel);
		}
		// PRODUCT SALES
		else if (SalesTasks.PRODUCT_SALES.getId().equals(id)) {
			if (productSalesPanel == null)
				productSalesPanel = new ProductSalesReportPanel();
			activatePanel(parentPanel, productSalesPanel);
		}
		// MONTHLY GROUP  SALES
		else if (SalesTasks.MONTHLY_GROUP_SALES.getId().equals(id)) {
			if (monthlyGroupSalesPanel == null)
				monthlyGroupSalesPanel = new MonthlyGroupSalesReportPanel();
			activatePanel(parentPanel, monthlyGroupSalesPanel);
		}
		// SALESMAN SALES
		else if (SalesTasks.SALESMAN_SALES.getId().equals(id)) {
			if (salesmanSalesReportPanel == null)
				salesmanSalesReportPanel = new SalesmanSalesReportPanel();
			activatePanel(parentPanel, salesmanSalesReportPanel);
		}
		// INDIV SALESMAN SALES
		else if (SalesTasks.INDIV_SALESMAN_SALES.getId().equals(id)) {
			if (indivSalesmanSalesReportPanel == null)
				indivSalesmanSalesReportPanel = new IndividualSalesmanSalesReportPanel();
			activatePanel(parentPanel, indivSalesmanSalesReportPanel);
		}
		// INDIV SALESMAN COMM
		else if (SalesTasks.INDIV_SALESMAN_COMM.getId().equals(id)) {
			if (indivSalesmanCommReportPanel == null)
				indivSalesmanCommReportPanel = new IndividualSalesmanCommissionReportPanel();
			activatePanel(parentPanel, indivSalesmanCommReportPanel);
		}
		// OUTSTANDING RECEIVABLES
		else if (SalesTasks.OUTSTANDING_RECEIVABLES.getId().equals(id)) {
			if (outstandingReceivablesPanel == null)
				outstandingReceivablesPanel = new OutstandingReceivablesReportPanel();
			activatePanel(parentPanel, outstandingReceivablesPanel);
		}
		// Delivery Receipts Report
		else if (SalesTasks.DELIVERY_RECEIPTS_REPORT.getId().equals(id)) {
			if (deliveryReceiptsReportPanel == null)
				deliveryReceiptsReportPanel = new DeliveryReceiptsReportPanel();
			activatePanel(parentPanel, deliveryReceiptsReportPanel);
		}
		// INDIVIDUAL SOA
		else if (SalesTasks.INDIVIDUAL_SOA.getId().equals(id)) {
			if (individualSOAPanel == null)
				individualSOAPanel = new IndividualSOAReportPanel();
			activatePanel(parentPanel, individualSOAPanel);
		}
		// CUSTOMER AGING ANALYSIS
		else if (SalesTasks.CUSTOMER_AGING_ANALYSIS.getId().equals(id)) {
			if (customerAgingReportPanel == null)
				customerAgingReportPanel = new CustomerAgingReportPanel();
			activatePanel(parentPanel, customerAgingReportPanel);
		}
		// COMMISSION REPORT
		else if (SalesTasks.COMMISSION_REPORT.getId().equals(id)) {
			if (commissionReportPanel == null)
				commissionReportPanel = new CommissionReportPanel();
			activatePanel(parentPanel, commissionReportPanel);
		}
		else {
			
		}
	}
	
	protected void activatePanel(TabPanel tabPanel, ITabItemPanel panel) {
		tabPanel.add(panel, panel.getConfig());
		panel.loadData();
		tabPanel.setActiveWidget(panel);
	}
	
	// %%
	
	protected void activatePanel(CardLayoutContainer tabPanel, ITabItemPanel panel) {
		tabPanel.add(panel);
		panel.loadData();
		tabPanel.setActiveWidget(panel);
	}
	
	protected void activatePanel(Component tabPanel, ITabItemPanel panel) {
		if (tabPanel instanceof TabPanel)
			activatePanel((TabPanel) tabPanel, panel);
		else
			activatePanel((CardLayoutContainer) tabPanel, panel);
	}

}
